<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2019/5/27
 * Time: 10:08
 */
namespace Modules\Api\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Goods;
use App\Models\GoodsColor;
use App\Models\GoodsSku;
use App\Models\ShopCar;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ShopcarController extends Controller{
    //加入购物车
    public function shopcarAdd(Request $request){
        try{
            $openid = $request->input("openid",false);
            $goodsid = $request->input("goods_id",false);
            $skuid = $request->input("sku_id",false);
            $colorid = $request->input("color_id",false);
            if (!$goodsid || !$openid || !$skuid || !$colorid)return self::error(1000);
            //查询用户
            $user = User::where("openid",$openid)->first();
            if (!$user)return self::error(1002);
            //查询商品
            $goods = Goods::find($goodsid);
            if (!$goods)return self::error(1005);
            //查询规格和颜色
            $sku = GoodsSku::find($skuid);
            if (!$sku)return self::error(1006);
            $color = GoodsColor::find($colorid);
            if (!$color)return self::error(1007);
            //查询购物车
            $shopcar = ShopCar::where("user_id",$user->id)->where("goods_id",$goodsid)->where("sku_id",$skuid)->where("color_id",$colorid)->first();
            if (!$shopcar) {
                //实例化对象
                $shopcar = new ShopCar();
                $shopcar->user_id = $user->id;
                $shopcar->goods_id = $goods->id;
                $shopcar->sku_id = $sku->id;
                $shopcar->color_id = $color->id;
                $shopcar->goods_num = 1;
                $shopcar->amount = $sku->sku_price;
                $shopcar->score = $sku->sku_score;
                $shopcar->created_at = Carbon::now();
                $shopcar->save();
            }else{
                $shopcar->goods_num = $shopcar->goods_num+1;
                $shopcar->save();
            }
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //删除购物车
    public function shopcarDelete(Request $request){
        try{
            $id = $request->input("id",false);
            if (!$id)return self::error(1000);
            $shopcar = ShopCar::find($id);
            if (!$shopcar)return self::error(1003);
            //删除购物车
            $shopcar->delete();
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //修改购物车商品数量
    public function shopcarUpdate(Request $request){
        try{
            $id = $request->input("id",false);
            $type = $request->input("value",false);
            if (!$id || !$type || ($type!='puls' && $type!='minus'))return self::error(1000);
            //查询购物车
            $shopcar = ShopCar::where("id",$id)->with('goodsSku')->first();
            if (!$shopcar)return self::error(1003);
            //查询商品价格
            if ($type=="puls"){
                $shopcar->goods_num = $shopcar->goods_num+1;
                $shopcar->amount = ($shopcar->amount)+($shopcar->goodsSku->sku_price);
                $shopcar->score = ($shopcar->score)+($shopcar->goodsSku->sku_score);
                $shopcar->save();
            }elseif ($type=="minus"){
                if ($shopcar->goods_num>1){
                    $shopcar->goods_num = $shopcar->goods_num-1;
                    $shopcar->amount = ($shopcar->amount)-($shopcar->goodsSku->sku_price);
                    $shopcar->score = ($shopcar->score)-($shopcar->goodsSku->sku_score);
                    $shopcar->save();
                }else{
                    return self::error(1009);
                }
            }else{
                $shopcar->goods_num = $type;
                $shopcar->save();
            }
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //查询购物车
    public function goodscarList(Request $request){
        try{
            $openid = $request->input("openid",false);
            if (!$openid)return self::error(1000);
            $user = User::where("openid",$openid)->first();
            if (!$user)return self::error(1002);
            $shopcarData = ShopCar::where("user_id",$user->id)->with("goods")->with("goodsSku")->with("goodsColor")->orderBy("id","desc")->get();
            if ($shopcarData->count()==0)return self::error(1003);
            return self::success($shopcarData);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //选中购物车
    public function shopCarSelected(Request $request){
        try{
            $openid = $request->input("openid",false);
            $shopId = $request->input("id",0);
            $operation = $request->input("operation");//yes和no
            if (!$openid)return self::error(1000);
            $user = User::where("openid",$openid)->first();
            if (!$user)return self::error(1002);
            if (!$shopId || $shopId==0){
                if ($operation) {
                    if ($operation == 'yes') {
                        ShopCar::where("user_id", $user->id)->update(["selected" => 1]);
                    } else {
                        ShopCar::where("user_id", $user->id)->update(["selected" => 0]);
                    }
                }else{
                    return self::error(1012);
                }
            }else{
                $shop = ShopCar::find($shopId);
                if (!$shop)return self::error(1003);
                if ($shop->selected==1){
                    $shop->selected=0;
                    $shop->save();
                }else{
                    $shop->selected = 1;
                    $shop->save();
                }
            }
            return self::success();
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //结算
    public function shopCarSettlement(Request $request){
        try{
            $openid = $request->input("openid",false);
            $orderType = $request->input("js_type","shopcar");//shopcar和buy
            if (!$openid)return self::error(1000);
            $user = User::where("openid",$openid)->first();
            if (!$user)return self::error(1002);
            if ($orderType=='shopcar') {
                //查询购物车
                $shopcar = ShopCar::where("user_id", $user->id)->where("selected", 1)->with("goods")->with("goodsSku")->with("goodsColor")->get();
                $goods = [];
                $amount = 0;
                $score = 0;
                if ($shopcar->count() > 0) {
                    foreach ($shopcar as $item) {
                        $amount += $item['amount'];
                        $score += $item['score'];
                        $goods[] = ["id" => $item['id'],"goods_num" => $item['goods_num'], "amount" => $item['goodsSku']['sku_price'],"score"=>$item['goodsSku']['sku_score'],"goods_id"=>$item['goods']['id'], "goods_name" => $item["goods"]["goods_name"], "image" => $item["goods"]["image"], "sku_id"=>$item['goodsSku']['id'],"sku_name" => $item['goodsSku']['sku_name'], "color_id"=>$item['color_id'],"color_name" => $item["goodsColor"]["color_name"]];
                    }
                    return self::success(["goods" => $goods, "total" => (string)$amount,"score"=>(string)$score,"user_score"=>$user->score]);
                } else {
                    return self::error(1003);
                }
            }elseif($orderType=='buy'){
                $goodsId = $request->input("goods_id",false);
                $skuId = $request->input("sku_id",false);
                $colorId = $request->input("color_id",false);
                if (!$goodsId || !$skuId || !$colorId)return self::error(1000);
                $goods = Goods::find($goodsId);
                $sku = GoodsSku::find($skuId);
                $color = GoodsColor::find($colorId);
                if (!$goods || !$sku || !$color)return self::error(1005);
                $array[] = ["id"=>0,"goods_id"=>$goods['id'],"goods_num" => 1, "amount" => $sku['sku_price'], "score"=>$sku['sku_score'],"goods_name" => $goods['goods_name'], "image" => $goods['image'], "sku_id"=>$sku['id'] ,"sku_name" => $sku['sku_name'], "color_id"=>$color['id'],"color_name" => $color['color_name']];
                return self::success(["goods" => $array, "total" => (string)$sku['sku_price'],"score"=>(string)$sku['sku_score'],"user_score"=>$user->score]);
            }else{
                return self::error(1014);
            }
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }
}